package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:9999")
	if err != nil {
		panic(err)
	}

	scanner := os.Stdin

	for {
		buf := make([]byte, 512)
		cnt, err := scanner.Read(buf)
		if err != nil {
			panic(err)
		}
		log.Printf("stdin %s len %d", buf, cnt)
		// 写出的时候,  需要指定写出长度
		_, err = conn.Write(buf[:cnt])
		if err != nil {
			panic(err)
		}

		rBuf := make([]byte, 512)
		rcnt, err := conn.Read(rBuf)
		if err != nil {
			panic(err)
		}
		log.Printf("conn %s len %d\n", rBuf, rcnt)
	}
}
